package com.bawan.ddbb.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.comparator.ComparableComparator;

import java.io.File;
import java.io.FilenameFilter;
import java.util.*;

/**
 * Desc:  配置文件读取工具类：单例模式：实现动态刷新功能
 * Author: zhengcongchun
 * CreateTime: 2014-06-06 16:59
 * LastModifyTime: 2014-06-06 16:59
 * FinalModifier: zhengcongchun
 * Version: 1.0
 */
public class PropertiesUtil {
    private static final Log log = LogFactory.getLog(PropertiesUtil.class);

    private Properties properties = new Properties();

    private static List<String> configFilePaths = new ArrayList<String>();
    private static List<String> configFileSuffixes = new ArrayList<String>();

    private PropertiesUtil(){}

    private static class SingletonHolder {
        private static final PropertiesUtil INSTANCE = new PropertiesUtil();
    }

    private static PropertiesUtil getInstance(){
        return SingletonHolder.INSTANCE;
    }

    private void init(List<String> _configFilePaths, List<String> _configFileSuffixes) {
        if (null == _configFilePaths) configFilePaths = new ArrayList<String>();
        if (null != _configFilePaths && _configFilePaths.size() > 0) {
            configFilePaths.addAll(_configFilePaths);
        }

        if (null == _configFileSuffixes) configFileSuffixes = new ArrayList<String>();
        if (null != _configFileSuffixes && _configFileSuffixes.size() > 0) {
            configFileSuffixes.addAll(_configFileSuffixes);
        }
        if (configFileSuffixes.size() == 0) {
            configFileSuffixes.add(".properties");
        }

        log.debug("==========开始加载系统需要的配置文件，初始化 配置文件路径：" + _configFilePaths + ", 文件后缀：" + configFileSuffixes + "==========");

        List<File> configFiles = new ArrayList<File>(findFiles(configFilePaths, configFileSuffixes));

        Collections.sort(configFiles, new ComparableComparator<File>() {
            public int compare(File o1, File o2) {
                return (int) (o1.lastModified() - o2.lastModified());
            }
        });
    }

    private static Set<File> findFiles(List<String> paths, List<String> suffixes) {
        Set<File> cfgFiles = new HashSet<File>();
        final List<String> names = new ArrayList<String>(suffixes);
        for (String path : paths) {
            File file = new File(path);
            log.debug("==========存放配置文件目录为：" + file.getPath() + "==========");
            if (!file.isDirectory()) {
                continue;
            }

            File[] files = file.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    for (String suffix : names) {
                        if (name.toLowerCase().endsWith(suffix.toLowerCase())) {
                            return true;
                        }
                    }
                    return false;
                }
            });
            if (null == files) {
                continue;
            }
            for (File _file : files) {
                cfgFiles.add(file);
            }
        }

        return cfgFiles;
    }

    /**
     * 根据Key获取配置的属性值
     * @param key   属性
     * @param defaultValue  默认值
     * @return
     */
    public String getProperty(String key, String defaultValue) {
        String value = properties.getProperty(key);
        return (value == null) ? defaultValue : value;
    }
}
